<?php
namespace App\Http\Controllers\ApiUser;

use App\Service\Member\UserService;
use Illuminate\Http\Request;

class WxAuthController extends BaseController
{
    //微信授权

    protected $app_id = 'wx684d8a09d6ea29fd';
    protected $app_secret = '6c6ab5847b47b934931ff8522713c440';
    //protected $mch_id = '1698837235';

    public function __construct(UserService $service)
    {
        $this->db = $service;
    }

    /**
     * 用户详细授权，access_token
     */
    public function token(Request $request)
    {
        /**
         * GET（请使用https协议）
         * https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code
         */
        $appid = $this->app_id;
        $appsecret = $this->app_secret;
        $code = $request->post('code', '');
        $token = $request->post('token', '');
        if (!$token) {
            return $this->toJson(-2, 'token必须的');
        }
        if (!$code) {
            return $this->toJson(-2, 'CODE必须的');
        }
        if ($code == '0000') {
            $openid = $code;
        } else {
            $url = 'https://api.weixin.qq.com/sns/jscode2session';
            $url .= '?appid=' . $appid;
            $url .= '&secret=' . $appsecret;
            $url .= '&js_code=' . $code;
            $url .= '&grant_type=authorization_code';
            $resCurl = $this->wxCurl($url);
            if ($resCurl['code'] < 0) {
                return $this->toJson(-2, $resCurl['msg']);
            }
            $openid = $resCurl['data']['openid'];
        }
        $res = $this->db->openidByToken($token, $openid);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        $resData = [
            'openid' => $openid,
            'token' => $res['data'],
        ];
        return $this->toJson(0, '', $resData);
    }

    /**
     * 构建CURL请求，单线程
     * @param $url 请求地址
     * @param $string 参数拼接的字符串
     * @param $method 请求方式：put、post、get、delete
     * @return $json
     */
    public function wxCurl($url)
    {
        $curl = curl_init(); //初始化
        curl_setopt($curl,CURLOPT_URL,$url);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($curl, CURLOPT_TIMEOUT, 5);
        $output = curl_exec($curl);
        curl_close($curl);
        if (!$output) {
            return array('code'=>-1, 'msg'=>'请求失败');
        }
        $items = json_decode($output,true);
        return array('code' => 0, 'msg' => 'success', 'data' => $items);
    }

    //测试腾讯地图，位置转换
    public function mapByLocate(Request $request)
    {
        $locate = $request->post('locate', '');
        $obj = new \App\Utilitys\TxMap();
        $res = $obj->mapByLocate($locate);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //测试腾讯地图，距离计算
    public function mapByDistance(Request $request)
    {
        $from = $request->post('from', '');
        $to = $request->post('to', '');
        $mode = $request->post('mode', 'walking');
        $obj = new \App\Utilitys\TxMap();
        $res = $obj->mapByDistance($from, $to, $mode);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }
}